/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyBean;

import MyClass.DBConnector;
import MyClass.Product;
import MyClass.User;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author God
 */
public class ProductList {

    private ResultSet list;
    private Product latest;
    private Product product;
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Product getLatest() {
        return latest;
    }

    public void setLatest(Product latest) {
        this.latest = latest;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public void setList(ResultSet list) {
        this.list = list;
    }

//    String productID;
//    String userID;
//    String productName;
//    String description;
//    String imgURL;
//    int catID;
//    int price;
//    String type;
//
//    public String getProductID() {
//        return productID;
//    }
//
//    public void setProductID(String productID) {
//        this.productID = productID;
//    }
//
//    public String getUserID() {
//        return userID;
//    }
//
//    public void setUserID(String userID) {
//        this.userID = userID;
//    }
//
//    public String getProductName() {
//        return productName;
//    }
//
//    public void setProductName(String productName) {
//        this.productName = productName;
//    }
//
//    public String getDescription() {
//        return description;
//    }
//
//    public void setDescription(String description) {
//        this.description = description;
//    }
//
//    public String getImgURL() {
//        return imgURL;
//    }
//
//    public void setImgURL(String imgURL) {
//        this.imgURL = imgURL;
//    }
//
//    public int getCatID() {
//        return catID;
//    }
//
//    public void setCatID(int catID) {
//        this.catID = catID;
//    }
//
//    public int getPrice() {
//        return price;
//    }
//
//    public void setPrice(int price) {
//        this.price = price;
//    }
//
//    public String getType() {
//        return type;
//    }
//
//    public void setType(String type) {
//        this.type = type;
//    }
    public ResultSet getList() throws ClassNotFoundException, SQLException {

        Connection con = DBConnector.getConnection();
        Statement selectSt = con.createStatement();
        setList(selectSt.executeQuery("SELECT * FROM Product,Category Where Product.catID=Category.catID"));

        return list;
    }

    public Product SearchProduct(String productID) {
        try {
            Connection con = DBConnector.getConnection();
            Statement selectSt = con.createStatement();
            list = selectSt.executeQuery("SELECT * FROM Product,Category WHERE Product.catID=Category.catID AND Product.productID=" + productID);
            if (list.next()) {
                product = new Product(list.getString("productID"), list.getString("userID"), list.getString("productName"),
                        list.getString("description"), list.getString("imgURL"), list.getInt("price"),
                        list.getBoolean("type"), list.getString("category"));
            }
        } catch (ClassNotFoundException ex) {
            System.out.println(ex);
        } catch (SQLException ex) {
            System.out.println(ex);
        }
        return product;
    }
    
    public User SearchUser(String userID) {
        try {
            Connection con = DBConnector.getConnection();
            Statement selectSt = con.createStatement();
            list = selectSt.executeQuery("SELECT * FROM Product,UserList WHERE Product.userID=UserList.userID AND Product.userID=" + userID);
            if (list.next()) {
                user = new User(list.getString("userID"), list.getString("password"), list.getString("name"),
                        list.getInt("age"), list.getString("email"), list.getString("room"));                       
            }
        } catch (ClassNotFoundException ex) {
            System.out.println(ex);
        } catch (SQLException ex) {
            System.out.println(ex);
        }
        return user;
    }

    public ResultSet ListByCategory(String cat) throws ClassNotFoundException, SQLException {

        Connection con = DBConnector.getConnection();
        Statement selectSt = con.createStatement();
        setList(selectSt.executeQuery("SELECT * FROM Product,Category WHERE Product.catID=Category.catID AND Product.catID=" + cat));

        return list;
    }

    public ResultSet ListByUser(String userID) {
        try {
            Connection con = DBConnector.getConnection();
            Statement selectSt = con.createStatement();
            setList(selectSt.executeQuery("SELECT * FROM Product,Category WHERE Product.catID=Category.catID"
                    + "AND Product.userID=" + userID));
        } catch (SQLException ex) {
            Logger.getLogger(ProductList.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ProductList.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

    public Product getLatestProduct() throws ClassNotFoundException, SQLException {
        getList();
        list.last();
        latest = new Product(list.getString("productID"), list.getString("userID"), list.getString("productName"),
                list.getString("description"), list.getString("imgURL"), Integer.parseInt(list.getString("price")),
                list.getBoolean("type"), list.getString("category"));

        return latest;
    }
}
